package com.photoprint;


import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PhotoPrintActivity extends ActivityGroup implements OnClickListener{

	private LocalActivityManager localActivityManager;
	private FrameLayout linearLayout_activity_container;
	private LinearLayout linearLayout_tab_container;
	
	private TextView tab_main;
	private TextView tab_photo;
	private TextView tab_shop;
	private TextView tab_more;
	
	private int windowWidth = 0;
	private int windowHeight = 0;
	private DisplayMetrics dm = null; 
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
//		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.appmain);
		init();
		localActivityManager = getLocalActivityManager();
		selectTab(tab_main);
		setTabButtonState(tab_main);
    }
    
    @Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		dm = new DisplayMetrics();   
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		windowWidth = dm.widthPixels;
		windowHeight = dm.heightPixels;
	}


	@Override
	public void closeContextMenu() {
		// TODO Auto-generated method stub
		super.closeContextMenu();
	}


	@Override
	public void closeOptionsMenu() {
		// TODO Auto-generated method stub
		super.closeOptionsMenu();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater=getMenuInflater();
		inflater.inflate(R.menu.app_menu, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return true;
	}


	public void init(){
    	linearLayout_tab_container = (LinearLayout) findViewById(R.id.tab_container);
    	linearLayout_activity_container = (FrameLayout) findViewById(R.id.activity_container);
    	tab_main = (TextView) findViewById(R.id.tab_main);
    	tab_photo = (TextView) findViewById(R.id.tab_photo);
    	tab_shop = (TextView) findViewById(R.id.tab_shop);
    	tab_more = (TextView) findViewById(R.id.tab_more);
    	
    	tab_main.setOnClickListener(this);
    	tab_photo.setOnClickListener(this);
    	tab_shop.setOnClickListener(this);
    	tab_more.setOnClickListener(this);
    }
    
    /**
	 * select tab
	 * @param view
	 */
	public void selectTab(View view){
		switch (view.getId()) {
		case R.id.tab_main:
			linearLayout_activity_container.removeAllViews();
			linearLayout_activity_container.addView(localActivityManager.startActivity(getResources().getString(R.string.tab_main),
					new Intent(this,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
			break;
		case R.id.tab_photo:
			linearLayout_activity_container.removeAllViews();
			linearLayout_activity_container.addView(localActivityManager.startActivity(getResources().getString(R.string.tab_photo),
					new Intent(this,PhotoActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
//			LotteriesActivity
			break;
		case R.id.tab_shop:
			linearLayout_activity_container.removeAllViews();
			linearLayout_activity_container.addView(localActivityManager.startActivity(getResources().getString(R.string.tab_shop),
					new Intent(this,ShopActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView());
//					PlazaActivity
			break;
		case R.id.tab_more:
			this.openOptionsMenu();
			break;
		default:
			break;
		}
	}
	
	/**
	 * set tab state
	 */
	public void setTabButtonState(View view){
		view.setBackgroundColor(Color.GREEN);
		if (view.getId() == R.id.tab_main) {
			tab_photo.setBackgroundColor(Color.WHITE);
			tab_shop.setBackgroundColor(Color.WHITE);
			tab_more.setBackgroundColor(Color.WHITE);
		}else if (view.getId() == R.id.tab_photo) {
			tab_main.setBackgroundColor(Color.WHITE);
			tab_shop.setBackgroundColor(Color.WHITE);
			tab_more.setBackgroundColor(Color.WHITE);
		}else if (view.getId() == R.id.tab_shop) {
			tab_main.setBackgroundColor(Color.WHITE);
			tab_photo.setBackgroundColor(Color.WHITE);
			tab_more.setBackgroundColor(Color.WHITE);
		}else if(view.getId() == R.id.tab_more){
			tab_main.setBackgroundColor(Color.WHITE);
			tab_photo.setBackgroundColor(Color.WHITE);
			tab_shop.setBackgroundColor(Color.WHITE);
		}
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		selectTab(v);
		setTabButtonState(v);
	}
}